import * as Cesium from 'cesium/Cesium'
import getWorldPosition from "./getWorldPosition";
var positionScratch$e = new Cesium.Cartesian3
    , normalScratch$8 = new Cesium.Cartesian3
    , surfaceNormalScratch$1 = new Cesium.Cartesian3
    , scratchCartesian2s = [new Cesium.Cartesian2, new Cesium.Cartesian2, new Cesium.Cartesian2, new Cesium.Cartesian2]
    ,
    scratchCartesian3s = [new Cesium.Cartesian3, new Cesium.Cartesian3, new Cesium.Cartesian3, new Cesium.Cartesian3, new Cesium.Cartesian3];

function getSlope(e, t) {
    Cesium.Check.defined("scene", e),
        Cesium.Check.defined("windowCoordinates", t);
    var i = getSlope._getWorldPosition(e, t, positionScratch$e);
    if (Cesium.defined(i)) {
        var r = e.camera.position
            , n = Cesium.Cartesian3.distance(i, r);
        if (!(1e4 < n)) {
            var a = scratchCartesian3s[0]
                , o = scratchCartesian3s[1]
                , s = scratchCartesian3s[2]
                , l = scratchCartesian3s[3]
                , c = e.frameState.mapProjection.ellipsoid.geodeticSurfaceNormal(i, normalScratch$8);
            c = Cesium.Cartesian3.negate(c, c);
            var u = Cesium.Cartesian2.clone(t, scratchCartesian2s[0]);
            u.x -= 2,
                u.y -= 2;
            var d = Cesium.Cartesian2.clone(t, scratchCartesian2s[1]);
            d.x -= 2,
                d.y += 2;
            var h = Cesium.Cartesian2.clone(t, scratchCartesian2s[2]);
            h.x += 2,
                h.y += 2;
            var p = Cesium.Cartesian2.clone(t, scratchCartesian2s[3]);
            p.x += 2,
                p.y -= 2;
            var m, f, g, _, v = getSlope._getWorldPosition(e, u, a), y = getSlope._getWorldPosition(e, d, o),
                C = getSlope._getWorldPosition(e, h, s), S = getSlope._getWorldPosition(e, p, l);
            if (Cesium.defined(v)) {
                var T = Cesium.Cartesian3.subtract(v, i, a);
                m = Cesium.Cartesian3.magnitude(T) / n <= .05 ? Cesium.Cartesian3.normalize(T, a) : void 0
            }
            if (Cesium.defined(y)) {
                var x = Cesium.Cartesian3.subtract(y, i, o);
                f = Cesium.Cartesian3.magnitude(x) / n <= .05 ? Cesium.Cartesian3.normalize(x, o) : void 0
            }
            if (Cesium.defined(C)) {
                var E = Cesium.Cartesian3.subtract(C, i, s);
                g = Cesium.Cartesian3.magnitude(E) / n <= .05 ? Cesium.Cartesian3.normalize(E, s) : void 0
            }
            if (Cesium.defined(S)) {
                var b = Cesium.Cartesian3.subtract(S, i, l);
                _ = Cesium.Cartesian3.magnitude(b) / n <= .05 ? Cesium.Cartesian3.normalize(b, l) : void 0
            }
            var P = Cesium.Cartesian3.clone(Cesium.Cartesian3.ZERO, surfaceNormalScratch$1)
                , A = scratchCartesian3s[4];
            if (Cesium.defined(m) && Cesium.defined(f) && (A = Cesium.Cartesian3.normalize(Cesium.Cartesian3.cross(m, f, A), A),
                P = Cesium.Cartesian3.add(P, A, P)),
            Cesium.defined(f) && Cesium.defined(g) && (A = Cesium.Cartesian3.normalize(Cesium.Cartesian3.cross(f, g, A), A),
                P = Cesium.Cartesian3.add(P, A, P)),
            Cesium.defined(g) && Cesium.defined(_) && (A = Cesium.Cartesian3.normalize(Cesium.Cartesian3.cross(g, _, A), A),
                P = Cesium.Cartesian3.add(P, A, P)),
            Cesium.defined(_) && Cesium.defined(m) && (A = Cesium.Cartesian3.normalize(Cesium.Cartesian3.cross(_, m, A), A),
                P = Cesium.Cartesian3.add(P, A, P)),
                !P.equals(Cesium.Cartesian3.ZERO))
                return P = Cesium.Cartesian3.normalize(P, P),
                    Cesium.Math.asinClamped(Math.abs(Math.sin(Cesium.Cartesian3.angleBetween(P, c))))
        }
    }
}
getSlope._getWorldPosition = getWorldPosition;
export  default getSlope;